﻿namespace QuickMail.View.Windows
{
    using System;
    using System.Globalization;
    using System.IO;
    using System.Windows;
    using QuickMail.DataModels;
    using QuickMail.Properties;

    /// <summary>
    /// The common settings window.
    /// </summary>
    public partial class CommonSettingsWindow
    {
        readonly string _path = AppDomain.CurrentDomain.BaseDirectory + "Languages";
        private string[] _languageFiles;

        public CommonSettingsWindow()
        {
            InitializeComponent();
            SetCurrentTranslate();

            PariodTextBox.Text = (Settings.Default.CheckFoldersPeriod / 1000).ToString(CultureInfo.InvariantCulture);
            MaxLetterSizeTextBox.Text = (Settings.Default.MailSize / 1048576).ToString(CultureInfo.InvariantCulture);
            ShowNotificationsCheckBox.IsChecked = Settings.Default.ShowNotifications;
            SignedLettersTextBox.Text = Settings.Default.SignedLetters;
            GetLangFiles();
        }

        /// <summary>
        /// Sets the current translation for form elements.
        /// </summary>
        private void SetCurrentTranslate()
        {
            // ReSharper disable once RedundantThisQualifier
            this.Title = StaticFields.Lang.CommonSettingsWindow.Title;
            SelectLangTextBlock.Text = StaticFields.Lang.CommonSettingsWindow.SelectLang;
            PeriodTextBlock.Text = StaticFields.Lang.CommonSettingsWindow.Period;
            PariodTextBox.ToolTip = StaticFields.Lang.CommonSettingsWindow.PeriodTt;
            MaxLetterSizeTextBlock.Text = StaticFields.Lang.CommonSettingsWindow.MaxLetterSize;
            MaxLetterSizeTextBox.ToolTip = StaticFields.Lang.CommonSettingsWindow.MaxLetterSizeTt;
            ShowNotificationsTextBlock.Text = StaticFields.Lang.CommonSettingsWindow.ShowNotifications;
            ShowNotificationsCheckBox.ToolTip = StaticFields.Lang.CommonSettingsWindow.ShowNotificationsTt;
            SignedLettersTextBlock.Text = StaticFields.Lang.CommonSettingsWindow.SignedLetters;
            SignedLettersTextBox.ToolTip = StaticFields.Lang.CommonSettingsWindow.SignedLettersTt;
            ApplyButton.Content = StaticFields.Lang.CommonSettingsWindow.Apply;
            CancelButton.Content = StaticFields.Lang.CommonSettingsWindow.Cancel;
        }

        /// <summary>
        /// The apply click
        /// </summary>
        private void Apply_OnClick(object sender, RoutedEventArgs e)
        {
            int tempPeriod;
            if (!int.TryParse(PariodTextBox.Text, out tempPeriod) || tempPeriod < 30)
            {
                PariodTextBox.Background = StaticFields.ErrorColorBrush;
                PariodTextBox.Focus();
                return;
            }

            int tempMailSize;
            if (!int.TryParse(MaxLetterSizeTextBox.Text, out tempMailSize) || tempMailSize < 1 || tempMailSize > 500)
            {
                MaxLetterSizeTextBox.Background = StaticFields.ErrorColorBrush;
                MaxLetterSizeTextBox.Focus();
                return;
            }

            if (LanguageComboBox.Items.Count > 0 && LanguageComboBox.SelectedIndex != -1)
            {
                Settings.Default.LanguagePath = _languageFiles[LanguageComboBox.SelectedIndex];
            }

            Settings.Default.CheckFoldersPeriod = tempPeriod * 1000;     //! translate msec to sec.
            Settings.Default.MailSize = tempMailSize * 1048576;          //! translate bytes to megabytes
            Settings.Default.ShowNotifications = ShowNotificationsCheckBox.IsChecked == true;
            Settings.Default.SignedLetters = SignedLettersTextBox.Text;
            Settings.Default.Save();

            DialogResult = true;
            Close();
        }

        /// <summary>
        /// The cancel click
        /// </summary>
        private void CancelButton_OnClick(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }

        /// <summary>
        /// Gets the language files and indicates their names in LanguageComboBox
        /// </summary>
        private void GetLangFiles()
        {
            if (!Directory.Exists(_path))
                return;

            _languageFiles = Directory.GetFiles(_path);
            foreach (var languageFile in _languageFiles)
            {
                var extension = Path.GetExtension(languageFile);
                if (extension != null
                    && extension.ToLower() == ".xml")
                {
                    LanguageComboBox.Items.Add(Path.GetFileNameWithoutExtension(languageFile));
                }
            }

            LanguageComboBox.SelectedIndex = 0;
            LanguageComboBox.SelectedItem = Path.GetFileNameWithoutExtension(Settings.Default.LanguagePath);
        }
    }
}
